package com.youi.service.impl;

import com.google.common.collect.Maps;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.GoodsDao;
import com.youi.entity.Goods;
import com.youi.entity.Goodsandgoodscar;
import com.youi.entity.Goodscar;
import com.youi.entity.Goodsimgs;
import com.youi.entity.Goodssize;
import com.youi.entity.Merchant;
import com.youi.entity.Purchasehistory;
import com.youi.entity.User;
import com.youi.service.GoodsService;
import com.youi.service.GoodsandgoodscarService;
import com.youi.service.GoodscarService;
import com.youi.service.GoodsimgsService;
import com.youi.service.GoodssizeService;
import com.youi.service.MerchantService;
import com.youi.service.PurchasehistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService {

    private GoodsDao goodsDao;

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private GoodsimgsService goodsimgsService;
    @Autowired
    private PurchasehistoryService purchasehistoryService;
    @Autowired
    private GoodscarService goodscarService;
    @Autowired
    private GoodsandgoodscarService goodsandgoodscarService;
    @Autowired
    private GoodssizeService goodssizeService;

    @Autowired
    public void setfaultRecordDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
        super.setBaseDAO(goodsDao);
    }

    @Override
    public void getGoodsMsg(List<Goods> goodsList) {
        if (goodsList != null && goodsList.size() > 0) {
            for (Goods goods : goodsList) {
                //获取商品说属商家信息
                Merchant merchant = merchantService.get(goods.getMerchantId());
                goods.setMerchant(merchant);

                Map<String, Object> params = new HashMap<String, Object>();
                params.put(Common.DELSTATUS, DelStatus.NODEL);
                params.put("goodsId", goods.getId());
                Map<String, Object> sortPram = new HashMap<String, Object>();
                sortPram.put(Common.TYPE, Common.ASC);

                //获取商品图库
                List<Goodsimgs> goodsImgList = goodsimgsService.getByParamsList(params, null, sortPram, 0, 5, null, null);
                goods.setGoodsimgsList(goodsImgList);

                //商品库存，取商品下所有规格的库存
                List<Goodssize> goodssizeList = goodssizeService.getByParams(params);
                goods.setGoodssizeList(goodssizeList);
                Integer allStockNum = 0;
                for (Goodssize goodssize : goodssizeList) {
                    if (goodssize.getRealStockNum() != null) {
                        allStockNum += goodssize.getRealStockNum();
                    }
                }
                goods.setAllStockNum(allStockNum);

                //商品购买记录，只获取最新的6条记录，同一用户购买多次只算一次
				/*
				Map<String, Object> sortParams = new HashMap<String, Object>();
				sortParams.put(Common.ID, Common.DESC);
				List<Purchasehistory> purchasehistoryList = purchasehistoryService.getByParamsList(params, null, sortParams, 0, 6, null, null);
				goods.setPurchasehistoryList(purchasehistoryService.getPurchasehistoryMsg(purchasehistoryList));
				*/
                List<Purchasehistory> purchaseHistoryList = purchasehistoryService.listNoRepeatPurchaseUser(new HashMap<String, Object>(2) {{
                    put("goodsId", goods.getId());
                    put("limit", 6);
                }});
                goods.setPurchasehistoryList(purchaseHistoryList);
            }
        }
    }

    @Override
    public void getGoodsMsg(Goods goods, User user) {
        //获取商品说属商家信息
        Merchant merchant = merchantService.get(goods.getMerchantId());
        goods.setMerchant(merchant);
        //获取商品图库
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put("goodsId", goods.getId());
        Map<String, Object> sortPram = new HashMap<String, Object>();
        sortPram.put(Common.ID, Common.DESC);
        List<Goodsimgs> goodsimgsList = goodsimgsService.getByParamsList(params, null, sortPram, 0, 5, null, null);
        goods.setGoodsimgsList(goodsimgsList);
        //商品规格
        List<Goodssize> goodssizeList = goodssizeService.getByParams(params);
        goods.setGoodssizeList(goodssizeList);
        Integer allStockNum = 0;
        for (Goodssize goodssize : goodssizeList) {
            if (goodssize.getRealStockNum() != null) {
                allStockNum += goodssize.getRealStockNum();
            }
        }
        goods.setAllStockNum(allStockNum);
        //商品购买记录
        Map<String, Object> sortParams = new HashMap<String, Object>();
        sortParams.put(Common.ID, Common.DESC);
        List<Purchasehistory> purchasehistoryList = purchasehistoryService.getByParamsList(params, null, sortParams, 0, 50, null, null);
        goods.setPurchasehistoryList(purchasehistoryService.getPurchasehistoryMsg(purchasehistoryList));
        Integer saleNum = 0;
        if (purchasehistoryList != null && purchasehistoryList.size() > 0) {
            for (Purchasehistory purchasehistory : purchasehistoryList) {
                saleNum += purchasehistory.getCount();
            }
        }
        goods.setSaleNum(saleNum);
        //获取店铺所属其他商品
        params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put("merchantId", goods.getMerchantId());
        Map<String, Object> noParams = new HashMap<String, Object>();
        noParams.put(Common.ID, goods.getId());
        List<Goods> otherGoodsList = goodsDao.getNoObjectList(params, noParams);
        if (otherGoodsList != null && otherGoodsList.size() > 0) {
            for (Goods goods2 : otherGoodsList) {
                params = new HashMap<String, Object>();
                params.put(Common.DELSTATUS, DelStatus.NODEL);
                params.put("goodsId", goods.getId());
                params.put(Common.TYPE, 1);
                //商品图库
                goodsimgsList = goodsimgsService.getByParams(params);
                goods2.setGoodsimgsList(goodsimgsList);
            }
            goods.setOtherGoodsList(otherGoodsList);
        }
        //判断购物车是否有商品
        if (user != null) {
            params = new HashMap<String, Object>();
            params.put(Common.DELSTATUS, DelStatus.NODEL);
            params.put(Common.USERID, user.getId());
            Goodscar goodscar = goodscarService.getByParam(params);
            if (goodscar == null) {
                goodscar = new Goodscar(user.getId(), 0.00);
                goodscarService.saveOrUpdate(goodscar);
            }
            params = new HashMap<String, Object>();
            params.put(Common.DELSTATUS, DelStatus.NODEL);
            params.put("goodscarId", goodscar.getId());
            List<Goodsandgoodscar> goodsandgoodscars = goodsandgoodscarService.getByParams(params);
            if (goodsandgoodscars == null || goodsandgoodscars.size() == 0) {
                goods.setIsGoodsCar(0);
            } else {
                goods.setIsGoodsCar(1);
            }
        } else {
            goods.setIsGoodsCar(0);
        }
    }

    @Override
    public void getGoodsInfoById(Goods goods) {
        //获取商品说属商家信息
        Merchant merchant = merchantService.get(goods.getMerchantId());
        goods.setMerchant(merchant);

        Map<String, Object> params = Maps.newHashMap();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put("goodsId", goods.getId());

        Map<String, Object> sortPram = Maps.newHashMap();
        sortPram.put(Common.TYPE, Common.ASC);

        //获取商品图库
        List<Goodsimgs> goodsImgList = goodsimgsService.getByParamsList(params, null, sortPram, 0, 5, null, null);
        goods.setGoodsimgsList(goodsImgList);

        //商品库存，取商品下所有规格的库存
        List<Goodssize> goodsSizeList = goodssizeService.getByParams(params);
        goods.setGoodssizeList(goodsSizeList);

        Integer allStockNum = 0;
        for (Goodssize goodssize : goodsSizeList) {
            if (goodssize.getRealStockNum() != null) {
                allStockNum += goodssize.getRealStockNum();
            }
        }
        goods.setAllStockNum(allStockNum);

        //商品购买记录，只获取最新的6条记录，同一用户购买多次只算一次
        List<Purchasehistory> purchaseHistoryList = purchasehistoryService.listNoRepeatPurchaseUser(new HashMap<String, Object>(2) {{
            put("goodsId", goods.getId());
            put("limit", 6);
        }});
        goods.setPurchasehistoryList(purchaseHistoryList);
    }

    @Override
    public List<Goods> getByNoParams(Map<String, Object> params, Map<String, Object> noParams) {
        return goodsDao.getNoObjectList(params, noParams);
    }

    @Override
    public String getGoodsImg(Goods goods) {
        if (goods != null) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(Common.DELSTATUS, DelStatus.NODEL);
            params.put("goodsId", goods.getId());
            params.put(Common.TYPE, 1);
            Goodsimgs goodsimgs = goodsimgsService.getByParam(params);
            if (goodsimgs != null && goodsimgs.getImgs() != null && goodsimgs.getImgs().length() > 0) {
                return goodsimgs.getImgs();
            }
        }
        return null;
    }

    @Override
    public Goods getForUpdate(Long id) {
        return goodsDao.getForUpdate(id);
    }

    @Override
    public List<Goods> listGoodsByPage(Map<String, Object> params) {
        return goodsDao.listGoodsByPage(params);
    }

    @Override
    public Integer countListGoods(Map<String, Object> params) {
        return goodsDao.countListGoods(params);
    }

    @Override
    public Integer batchUpdateGoodsStatus(List<Long> goodsIds, Integer status) {
        return goodsDao.batchUpdateGoodsStatus(goodsIds, status);
    }

    @Override
    public List<Goods> listNoStockGoods(Map<String, Object> param) {
        return goodsDao.listNoStockGoods(param);
    }
}